﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Hotel
{
    public partial class Room : Form
    {
        public Room()
        {
            InitializeComponent();
        }
        CNDatabase dt = new CNDatabase();
        BindingSource bdS2 = new BindingSource();
        BindingSource bdS;
        private void Room_Load(object sender, EventArgs e)
        {
            loadData();
        }
      
        private void loadData()
        {
            try
            {
               
                grvRoom.AutoGenerateColumns = false;
                
                string qr2 = "select * from Room,TypeRoom  where Room.IDTypeRoom = TypeRoom.IDTypeRoom";
                bdS2.DataSource = dt.getDataset(qr2).Tables[0];
                bindingNavigator1.BindingSource = bdS2;
                grvRoom.DataSource = bdS2;
                
                this.TbRoomName.DataBindings.Add("Text", bdS2, "NameRoom");
                this.TbIDRoom.DataBindings.Add("Text", bdS2, "IDRoom");
                this.TbDes.DataBindings.Add("Text", bdS2, "Description");
                this.cbType.DataBindings.Add("SelectedValue", bdS2, "IDTypeRoom");

                string qr = "select * from TypeRoom";
                bdS = dt.getData(qr);
                cbType.DataSource = bdS;
                cbType.DisplayMember = "NameType";
                cbType.ValueMember = "IDTypeRoom";

            }
            catch (Exception)
            {
                dt.closeConnect();
            }

        }

       

        private void TbRoomName_KeyPress(object sender, KeyPressEventArgs e)
        {
            TbIDRoom.Text = dt.getOneData("select max(IDRoom)+1 from Room").ToString();
            TbIDRoom.Text = "(Auto ID)";
        }

       
        private void BtAddNew_Click(object sender, EventArgs e)
        {
            try
            {
                string status = "";
                if (rb1.Checked)
                    status = "Available";
                if (rb2.Checked)
                    status = "Reservation";
                if (rb3.Checked)
                    status = "Staying";
                if (rb4.Checked)
                    status = "Maintaining";

                string qr = "INSERT INTO Room(NameRoom,IDTypeRoom,Status,Description) VALUES('"
                    + TbRoomName.Text + "', '" + cbType.SelectedValue + "', '" + status + "', '" + TbDes.Text + "')";
                dt.xlExecuteNonQuery(qr);
                MessageBox.Show("Inserted new room successfully !", "Information", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                grvRoom.DataSource = bdS2;
                loadData();

            }
            catch (Exception)
            {
                dt.closeConnect();
                MessageBox.Show("Try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        private void BtUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                string status = "";
                if (rb1.Checked)
                    status = "Available";
                if (rb2.Checked)
                    status = "Reservation";
                if (rb3.Checked)
                    status = "Staying";
                if (rb4.Checked)
                    status = "Maintaining";

                //string qr = "INSERT INTO Room(NameRoom,IDTypeRoom,Status,Description) VALUES('"
                //  + TbRoomName.Text + "', '" + cbType.SelectedValue + "', '" + status + "', '" + TbDes.Text + "')";


                string qr = "UPDATE Room SET [NameRoom] = '" + TbRoomName.Text + "',[IDTypeRoom] = '"
                    + cbType.SelectedValue + "',[Status] = '" + status + "',[Description] = '"
                    + TbDes.Text + "' WHERE IDRoom='" + TbIDRoom.Text + "'";
                dt.xlExecuteNonQuery(qr);
                MessageBox.Show("Updated room's information successfully !", "Information", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                grvRoom.DataSource = bdS2;
                
                // bindingNavigator1.BindingSource = bdS2;
                loadData();

            }
            catch (Exception)
            {
                dt.closeConnect();
                MessageBox.Show("Try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        private void LbClear_Click(object sender, EventArgs e)
        {
            TbIDRoom.Text = "";
            TbRoomName.Text = "";
            TbDes.Text = "";
            cbType.Text = "";

        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            dt.xlExecuteNonQuery("DELETE FROM [Room] WHERE IDRoom='"+TbIDRoom.Text+"'");
           // grvRoom.DataSource = bdS2;
            MessageBox.Show("Delete success");
            loadData();
        }

        
       

      

        

        
    }
}
